package com.lv.service.adapter

import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import cc.shinichi.library.ImagePreview
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lv.common.utils.MyUtils.mContext
import com.lv.common.widget.video.YGXVideoTopPlayer
import com.lv.service.R
import com.lv.service.bean.ViewTypeStringBean

/**
 * 作者： ygx
 * 创建日期：2022/8/3 11:13 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 *
 */
class MediaTypeAdapter(data: MutableList<ViewTypeStringBean>?) :
    BaseMultiItemQuickAdapter<ViewTypeStringBean, BaseViewHolder>(data) {
    private var myVideo: YGXVideoTopPlayer? = null

    init {
        //必须绑定type和layout的关系
        addItemType(ViewTypeStringBean.PIC_TYPE, R.layout.item_image_pre)
        addItemType(ViewTypeStringBean.VEDIO_TYPE, R.layout.item_video_pre)
    }

    override fun convert(helper: BaseViewHolder, item: ViewTypeStringBean) {
        helper ?: return
        item ?: return
        when (helper.itemViewType) {
            ViewTypeStringBean.PIC_TYPE -> {
                bindViewPic(helper, item.data)
            }
            ViewTypeStringBean.VEDIO_TYPE -> {
                bindViewVideo(helper, item.data)
            }
            else -> print("其他布局")
        }
    }

    private fun bindViewPic(helper: BaseViewHolder, item: String) {
        val mIvPhoto = helper.getView<com.luck.picture.lib.photoview.PhotoView>(R.id.iv_photo)
        val options = RequestOptions()
            .error(ImagePreview.instance.errorPlaceHolder)
            .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
            .format(DecodeFormat.PREFER_RGB_565)
        Glide.with(mContext).load(item).apply(options).into(mIvPhoto)

    }

    private fun bindViewVideo(helper: BaseViewHolder, item: String) {
        val clParent = helper.getView<ConstraintLayout>(R.id.cl_parent)

        myVideo = helper.getView<YGXVideoTopPlayer>(R.id.video_play)
//        val ivPlayTag = helper.getView<ImageView>(R.id.iv_image)
//        ivPlayTag.visibility = View.GONE
        myVideo?.setUpLazy(item, true, mContext.cacheDir, null, "")
//        val imageView = ImageView(context)
//        Glide.with(context).load(item.imageFirst).into(imageView)
//        myVideo?.thumbImageView = imageView
        myVideo?.titleTextView?.visibility = View.GONE
        myVideo?.backButton?.visibility = View.GONE
        myVideo?.fullscreenButton?.visibility = View.GONE
        myVideo?.isLooping = true
//        myVideo?.startButton?.performClick()
        myVideo?.isRotateViewAuto = false
        myVideo?.isLockLand = true
        myVideo?.isShowFullAnimation = true
        //音频焦点冲突时是否释放
        myVideo?.isReleaseWhenLossAudio = true
        //禁止全屏
        myVideo?.isAutoFullWithSize = true
        //禁止滑动
        myVideo?.setIsTouchWiget(true)
        //静音
//        GSYVideoManager.instance().isNeedMute = true
        clParent.setOnClickListener { myVideo?.fullscreenButton?.performClick() }
    }

    //释放视频播放器
    fun setVideoStop() {
        myVideo?.release()
    }

    fun getMyvideo(): YGXVideoTopPlayer? {
        return myVideo
    }
}
